const express = require('express');
const cookiesParser = require('cookie-parser');
const cookieSession = require('cookie-session');
const userService = require('../services/userService');

var router = express.Router();

router.use('/loginPage.html',async function (req,res,next) {
    var user = req.cookies['user'];
    console.log(user);
    if(user== {}||user==undefined){
        next();
    }else {
        var data = await userService.loginCookie(user);
        if(data.success){
            req.session.user = data.user;
            res.redirect('/views/pages/user1.html');
        }else {
            next();
        }
    }
})


//views路由上做 登录使用控制
//要进入pages下面的页面，必须是登录状态，否则全部进入/views/loginPage.html
router.use('/pages',(req,res,next)=>{
    var user = req.session['user'];
    if(user==null||user=={}||user==undefined){
        res.redirect('/views/loginPage.html');
    }else{
        next();
    }
})
exports.router = router;